package com.cslg.ac.demo.service.auth.mail;

import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.dao.user.person.PersonInfoDAO;
import com.cslg.ac.demo.dao.user.team.TeamInfoDAO;
import com.cslg.ac.demo.dao.user.UserAuthDAO;
import com.cslg.ac.demo.dao.user.UserDAO;
import com.cslg.ac.demo.dao.user.po.person.PersonInfoPO;
import com.cslg.ac.demo.dao.user.po.team.TeamInfoPO;
import com.cslg.ac.demo.dao.user.po.UserAuthPO;
import com.cslg.ac.demo.entity.email.Email;
import com.cslg.ac.demo.entity.email.EmailAddress;
import com.cslg.ac.demo.util.communicate.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;


@Service
@PropertySource(value = {"classpath:auth.properties"},encoding="utf-8")
public class MailService {

    @Autowired
    UserDAO userDAO;
    @Autowired
    JavaMailSender mailSender;
    @Autowired
    UserAuthDAO userAuthDAO;
    @Autowired
    TeamInfoDAO teamInfoDAO;
    @Autowired
    PersonInfoDAO personInfoDAO;
    @Value("${fromMail}")
    String fromMail;

    @Value("${serverUrl}")
    private String serverUrl;
    @Value("${tryAginWaitSecond}")
    private int tryAginWaitSecond;
    @Value("${validateSecond}")
    private int validateSecond;
    @Value("${active.title:}")//这个active就是在，在这定义了，实际值从传入中得到
    private String title;
    @Value("${active.content:}")
    private String content;
    @Value("${address}")
    private String address;
    @Value("${password}")
    private String password;
    @Value("${contentType}")
    String contentType;
    @Value("${encoding}")
    private String encoding;
    @Value("${isAuth}")
    private int isAuth;

    public int isAuth(int id){
        UserAuthPO userAuthPO = userAuthDAO.selectById(id);
        if(userAuthPO == null){
            return ResponseCode.USER_NOT_EXISTS;
        }
        if(userAuthPO.getEmailAuth() == 1){
            return ResponseCode.USER_NOT_AUTH;
        }else if(userAuthPO.getEmailAuth() == 0){
            return ResponseCode.ALREADY_HAVE_AUTH;
        }
        return ResponseCode.USER_NOT_AUTH;
    }

    public int authEmail(String content,EmailAddress... to)
    {


        for(EmailAddress emailAddress:to){
            if(!EmailUtil.isMailAddress(emailAddress.getAddress())){
                return ResponseCode.PARAM_ILLEGAL;
            }
            //如果邮箱已经验证
            PersonInfoPO personInfoPO = personInfoDAO.selectByMail(emailAddress.getAddress());
            TeamInfoPO teamInfoPO = teamInfoDAO.selectByMail(emailAddress.getAddress());
            UserAuthPO userAuthPO;
            if(personInfoPO != null){
                userAuthPO = userAuthDAO.selectById(personInfoPO.getId());
                if(userAuthPO.getEmailAuth() == isAuth){
                    return ResponseCode.ALREADY_HAVE_AUTH;
                }
            }
            if(teamInfoPO != null){
                userAuthPO = userAuthDAO.selectById(teamInfoPO.getId());
                if(userAuthPO.getEmailAuth() == isAuth){
                    return ResponseCode.ALREADY_HAVE_AUTH;
                }
            }
        }
        String title = "lovingpet邮箱验证";
        EmailAddress from = new EmailAddress("lovingpet","lian@lovingpet.top");
        Email email = new Email()
                .from(from)
                .to(to)
                .title(title)
                .content(content)
                .contentType(contentType)
                .encoding(encoding);
        try
        {
            EmailUtil.sendEmail(address, password, email);
        }
        catch (Exception e)
        {
           e.printStackTrace();
           return ResponseCode.SERVER_ERROR;
        }
        return ResponseCode.REQUEST_SUCCESSED;
    }

    public int authSucceed(String email,String phone){
        PersonInfoPO personInfoPO = personInfoDAO.selectByPhone(phone);
        TeamInfoPO teamInfoPO = teamInfoDAO.selectByPhone(phone);
        UserAuthPO userAuthPO = userAuthDAO.selectByName(phone);
        int id = userAuthPO.getId();
        UserAuthPO userAuthPO1 = userAuthDAO.selectById(id);
        userAuthPO1.setEmailAuth(0);
        userAuthPO1.setPhoneAuth(0);
            userAuthDAO.update(userAuthPO1);
            if(personInfoPO != null){
                personInfoPO.setEmail(email);
                personInfoDAO.updateAllById(personInfoPO);
            }else {
                teamInfoPO.setEmail(email);
                teamInfoDAO.updateBasicById(teamInfoPO);
            }
            return ResponseCode.REQUEST_SUCCESSED;
    }


}
